import os
from folium import plugins
import folium


LON_REVISION = 0.0054369220  # 经纬度的修正值
LA_REVISION = 0.0016467053 

def read_gps():
    point_list = []  # 坐标路径集合
    station_list = [] # 到站离站的点集合
    station_name = [] # 站点名称

    '''
    找点
    '''
    with open('gpsdata.txt', 'r') as f:
        lines = f.readlines()
        count = 0
        for line in lines:
            print(count)
            count += 1
            line = line.strip('\n')
            if '经度' in line and '纬度' in line:
                #print(line)
                line_list = line.split(' ')
                #print(line_list)
                longitude = 0.0
                latitude = 0.0
                for i in range(len(line_list)):
                    if '经度' in line_list[i]:
                        print(line_list[i+1])
                        longitude = float(line_list[i+1]) - LON_REVISION
                    if line_list[i] == '纬度':
                        print(line_list[i+2])
                        latitude = float(line_list[i+2]) - LA_REVISION
                point = [latitude, longitude]
                if latitude > 0 and longitude > 0:
                    point_list.append(point)

    with open('stationdata.txt', 'r') as f2:
        lines2 = f2.readlines()
        for line in lines2:
            line = line.strip('\n')
            if '站点' in line:
                line_list = line.split(' ')
                for i in range(len(line_list)):
                    if '到达' in line_list[i]:
                        name = '到达 '+line_list[i+1]
                        station_name.append(name)
                    if '离开' in line_list[i]:
                        name = '离开 '+line_list[i+1]
                        station_name.append(name)

            if '经度' in line and '纬度' in line:
                line_list = line.split(' ')
                longitude = 0.0
                latitude = 0.0
                for i in range(len(line_list)):
                    if '经度' in line_list[i]:
                        print(line_list[i+1])
                        longitude = float(line_list[i+1]) - LON_REVISION
                    if line_list[i] == '纬度':
                        print(line_list[i+2])
                        latitude = float(line_list[i+2]) - LA_REVISION
                point = [latitude, longitude]
                if latitude > 0 and longitude > 0:
                    station_list.append(point)
    '''
    画图
    '''
    #print(point_list)
    m = folium.Map([45.73823263241369, 126.63277248522367], zoom_start=10) #中心区域的确定
    #polyline方法为将坐标用线段形式连接起来  #将坐标点连接起来 #线的大小为3 #线的颜色为橙色 #线的透明度
    route = folium.PolyLine( point_list, weight=3, color='orange', opacity=0.8 ).add_to(m) #将这条线添加到刚才的区域m内

    # 画点
    for i in range(len(station_name)):
        folium.Marker(station_list[i], popup=parse_zhch(station_name[i])).add_to(m)
    m.save('Heatmap1.html') #将结果以HTML形式保存到桌面上


def parse_zhch(s):
    return str(str(s).encode('ascii' , 'xmlcharrefreplace'))[2:-1]





if __name__ == '__main__':
    read_gps()